var template = {

    tempSource: '',

    module:'',

    boxID: '',

    updateHtml: function() {
        $('#e_popup').find('.popup-content').html( template.createHtml() );
    },

    createHtml: function() {

        var module_data = template.tempSource;

        var html = '';

        if (this.module == 'product' || this.module == 'product_wap') {

            html += '<table>';

            if (module_data) {
                $.each(module_data, function(k, v) {
                    html += '<tbody>';
                    html += '<tr>';
                    html += '<td width="50">';
                    html += '<img src="' + v.img_link + '" width="50" height="50">';
                    html += '</td>';
                    html += '<td width="*">';
                    html += v.source_title;
                    html += '</td>';
                    html += '<td width="150">';
                    html += '<input class="btn source-delete" type="button" value="删除" index="' + k + '">';
                    if (k == 0) {
                        html += '&nbsp;&nbsp;<i class="up-btn no-up-btn" index="' + k + '"></i>';
                    } else {
                        html += '&nbsp;&nbsp;<i class="up-btn" index="' + k + '"></i>';
                    }
                    if (k == module_data.length - 1) {
                        html += '&nbsp;&nbsp;<i class="down-btn no-down-btn" index="' + k + '"></i>';
                    } else {
                        html += '&nbsp;&nbsp;<i class="down-btn" index="' + k + '"></i>';
                    }
                    html += '</td>';
                    html += '</tr>';
                    html += '</tbody>';
                });

            }

            html += '<tbody>';
            html += '<tr>';
            html += '<td>&nbsp;</td>';
            html += '<td><input type="text" id="product_code" value="请输入商品SKU，按回车键查询" style="width: 200px;color: #ccc;padding: 5px;"></td>';
            html += '<td>&nbsp;</td>';
            html += '</tr>';
            html += '</tbody>';
            html += '</table>';

        } else if (this.module == 'flash' || this.module == 'flash_wap') {

            html += '<table>';

            if (module_data) {
                $.each(module_data, function(k, v) {

                    if (!v.img_link) {
                        v.img_link = '/quanliadmin/docroot/images/backend/comm_default.jpg';
                    }

                    html += '<tbody>';

                    html += '<tr>';
                    html += '<td width="50">图片：</td>';
                    html += '<td width="250">';
                    html += '<img src="' + v.img_link + '" width="50" height="50">';
                    html += '&nbsp;<input class="btn btn-primary upload_img" type="button" value="上传图片" index="' + k + '">';
                    html += '</td>';
                    html += '<td width="50">链接：</td>';
                    html += '<td width="*">';
                    html += '<input class="txt ebsig-link" type="text" value="' + v.url_link + '" style="width: 260px;padding: 5px;" index="' + k + '">';
                    html += '</td>';
                    html += '</tr>';

                    html += '<tr>';
                    html += '<td>标题：</td>';
                    html += '<td>';
                    html += '<input class="txt ebsig-title" type="text" value="' + v.source_title + '" style="width: 200px;padding: 5px;" index="' + k + '">';
                    html += '</td>';
                    html += '<td colspan="2">';
                    html += '<input class="btn source-delete" type="button" value="删除" index="' + k + '">';
                    if (k == 0) {
                        html += '&nbsp;&nbsp;<i class="up-btn no-up-btn" index="' + k + '"></i>';
                    } else {
                        html += '&nbsp;&nbsp;<i class="up-btn" index="' + k + '"></i>';
                    }
                    if (k == module_data.length - 1) {
                        html += '&nbsp;&nbsp;<i class="down-btn no-down-btn" index="' + k + '"></i>';
                    } else {
                        html += '&nbsp;&nbsp;<i class="down-btn" index="' + k + '"></i>';
                    }
                    html += '</td>';
                    html += '</tr>';
                    html += '</tbody>';

                });
            }

            html += '<tbody>';
            html += '<tr>';
            html += '<td colspan="2"><input class="btn btn-primary" id="img_add" type="button" value="添加图片"></td>';
            html += '</tr>';
            html += '</tbody>';

            html += '</table>';

        } else if (this.module == 'html') {

            html += '<div>html代码：</div>';
            if (module_data.length > 0) {
                var description = module_data[0]['description'];
                html += '<textarea id="template-description" style="width: 670px;height: 400px;">' + description + '</textarea>';
            } else {
                html += '<textarea id="template-description" style="width: 670px;height: 400px;"></textarea>';
            }


        }else if(this.module == 'nav' || this.module == 'nav_wap'){

            html += '<table>';

            if (module_data) {
                $.each(module_data, function(k, v) {

                    html += '<tbody>';

                    html += '<tr>';
                    html += '<td width="60">中文名称：</td>';
                    html += '<td width="250">';
                    html += '<input class="txt ebsig-title" type="text" value="' + v.source_title + '" style="width: 200px;padding: 5px;" index="' + k + '">';
                    html += '</td>';
                    html += '<td width="50">链接：</td>';
                    html += '<td width="*">';
                    html += '<input class="txt ebsig-link" type="text" value="' + v.url_link + '" style="width: 260px;padding: 5px;" index="' + k + '">';
                    html += '</td>';
                    html += '</tr>';

                    html += '<tr>';
                    html += '<td>法文名称：</td>';
                    html += '<td>';
                    html += '<input class="txt ebsig-description" type="text" value="' + v.description + '" style="width: 200px;padding: 5px;" index="' + k + '">';
                    html += '</td>';
                    html += '<td colspan="2">';
                    html += '<input class="btn source-delete" type="button" value="删除" index="' + k + '">';
                    if (k == 0) {
                        html += '&nbsp;&nbsp;<i class="up-btn no-up-btn" index="' + k + '"></i>';
                    } else {
                        html += '&nbsp;&nbsp;<i class="up-btn" index="' + k + '"></i>';
                    }
                    if (k == module_data.length - 1) {
                        html += '&nbsp;&nbsp;<i class="down-btn no-down-btn" index="' + k + '"></i>';
                    } else {
                        html += '&nbsp;&nbsp;<i class="down-btn" index="' + k + '"></i>';
                    }
                    html += '</td>';
                    html += '</tr>';
                    html += '</tbody>';

                });
            }

            html += '<tbody>';
            html += '<tr>';
            html += '<td colspan="2"><input class="btn btn-primary" id="nav_add" type="button" value="添加菜单"></td>';
            html += '</tr>';
            html += '</tbody>';

            html += '</table>';

        }else if(this.module == 'flash_wap_new_product'){

            html += '<table>';

            if (module_data) {
                $.each(module_data, function(k, v) {

                    if (!v.img_link) {
                        v.img_link = '/quanliadmin/docroot/images/backend/comm_default.jpg';
                    }

                    html += '<tbody>';

                    html += '<tr>';
                    html += '<td width="50">图片：</td>';
                    html += '<td width="250">';
                    html += '<img src="' + v.img_link + '" width="50" height="50">';
                    html += '&nbsp;<input class="btn btn-primary upload_img" type="button" value="上传图片" index="' + k + '">';
                    html += '</td>';
                    html += '<td width="50">链接：</td>';
                    html += '<td width="*">';
                    html += '<input class="txt ebsig-link" type="text" value="' + v.url_link + '" style="width: 260px;padding: 5px;" index="' + k + '">';
                    html += '</td>';
                    html += '</tr>';
                    html += '<tr>';
                    html += '<td>介绍</td>';
                    html += '<td colspan="3"><textarea class="ebsig-description" style="width: 580px;height: 60px;padding: 5px;" index="'+ k +'">'+ v.description +'</textarea></td>';
                    html += '</tr>';

                    html += '<tr>';
                    html += '<td>标题：</td>';
                    html += '<td>';
                    html += '<input class="txt ebsig-title" type="text" value="' + v.source_title + '" style="width: 200px;padding: 5px;" index="' + k + '">';
                    html += '</td>';
                    html += '<td colspan="2">';
                    html += '<input class="btn source-delete" type="button" value="删除" index="' + k + '">';
                    if (k == 0) {
                        html += '&nbsp;&nbsp;<i class="up-btn no-up-btn" index="' + k + '"></i>';
                    } else {
                        html += '&nbsp;&nbsp;<i class="up-btn" index="' + k + '"></i>';
                    }
                    if (k == module_data.length - 1) {
                        html += '&nbsp;&nbsp;<i class="down-btn no-down-btn" index="' + k + '"></i>';
                    } else {
                        html += '&nbsp;&nbsp;<i class="down-btn" index="' + k + '"></i>';
                    }
                    html += '</td>';
                    html += '</tr>';
                    html += '</tbody>';

                });
            }

            html += '<tbody>';
            html += '<tr>';
            html += '<td colspan="2"><input class="btn btn-primary" id="img_add" type="button" value="添加图片"></td>';
            html += '</tr>';
            html += '</tbody>';

            html += '</table>';

        }

        return html;

    },

    edit: {

        open: function() {

            if (template_source[template.module]) {

                template.tempSource = E.clone(template_source[template.module]);

            } else {

                template.tempSource = new Array();

            }

            var html = template.createHtml();

            E.popup.open({
                title: '内容修改',
                content: html,
                css: 'width: 700px',
                btnName: '保存修改',
                btnCall: 'template.edit.save();'
            });

        },

        save: function() {

            if (template.module == 'product') {

                template_source[template.module] = template.tempSource;

                if (template.tempSource.length == 0) {
                    E.alert('请录入商品信息');
                    return false;
                }

                var html = '<ul class="image-grid" id="list">';

                $.each(template.tempSource, function(k, v) {
                    html += '<li class="ms" data-id="id-' + (k + 1) + '">';
                    html += '<div class="pro_box">';
                    html += '<img src="' + v.img_link + '" width="216" height="216" />';
                    html += '<div class="mask">';
                    html += '<font>' + v.source_title + '</font>';
                    html += '<div class="Sweetness_box">';
                    html += '<samp>1</samp>';
                    html += '<span>甜度指数：</span>';
                    html += '<ul class="Sweetness">';
                    html += '<li></li>';
                    html += '</ul>';
                    html += '<div class="buy_icon">';
                    html += '<a href="#" class="tobuy"></a>';
                    html += '</div>';
                    html += '</div>';
                    html += '</div>';
                    html += '</div>';
                    html += '<div class="pro_title">';
                    if (v.tag_img_link) {
                        html += '<img src="' + v.tag_img_link + '" width="30" height="30">';
                    }
                    html += '<span class="font01">' + v.frenchName + '</span>';
                    html += '<span>' + v.spec + '/' + v.price + 'RMB</span>';
                    html += '</div>';
                    html += '</li>';
                });

                html += '</ul>';

            } else if (template.module == 'product_wap') {

                template_source[template.module] = template.tempSource;

                if (template.tempSource.length == 0) {
                    E.alert('请录入商品信息');
                    return false;
                }

                var html = '<div class="pro_title"><span>热门商品 <small>Hot Sale</small></span></div>';
                html += '<ul class="list">';

                $.each(template.tempSource, function(k, v) {
                    html += '<li>';
                    html += '<div>';
                    html += '<a href="' + v.url_link + '"><img src="' + v.img_link + '" width="100%" /></a>';
                    html += '<a class="to_buy">立即订购</a>';
                    html += '</div>';
                    html += '<p>';
                    html += '<b>' + v.frenchName + '</b><br>' + v.source_title + '';
                    html += '</p>';
                });

                html += '</ul>';

            } else if (template.module == 'flash') {

                template_source[template.module] = template.tempSource;

                if (template.tempSource.length == 0) {
                    E.alert('请录入图片信息');
                    return false;
                }

                var error = false;
                var html = '<div class="Slidebox">';
                html += '<ul class="pic">';
                $.each(template.tempSource, function(k, v) {

                    var line = k + 1;

                    if (v.img_link == '/quanliadmin/docroot/images/backend/comm_default.jpg' || v.img_link == '') {
                        E.alert('请上传第' + line + '行图文的图片');
                        error = true;
                        return false;
                    }

                    if (v.link == '') {
                        E.alert('请上传第' + line + '行图文的链接');
                        error = true;
                        return false;
                    }

                    if (v.source_title == '') {
                        E.alert('请上传第' + line + '行图文的标题');
                        error = true;
                        return false;
                    }

                    html += '<li>';
                    html += '<div>';
                    html += '<a href="' + v.url_link + '" target="_blank">';
                    html += '<img src="' + v.img_link + '" />';
                    html += '</a>';
                    html += '</div>';
                    html += '</li>';

                });

                if (error) {
                    return false;
                }

                html += '</ul>';
                html += '<a class="prev" href="javascript:void(0)"></a>';
                html += '<a class="next" href="javascript:void(0)"></a>';
                html += '<div class="num"><ul></ul></div>';
                html += '</div>';


            } else if (template.module == 'flash_wap') {

                template_source[template.module] = template.tempSource;

                if (template.tempSource.length == 0) {
                    E.alert('请录入图片信息');
                    return false;
                }

                var error = false;
                var html = '<div class="swipe" id="mySwipe">';
                html += '<div class="swipe-wrap">';
                $.each(template.tempSource, function(k, v) {

                    var line = k + 1;

                    if (v.img_link == '/quanliadmin/docroot/images/backend/comm_default.jpg' || v.img_link == '') {
                        E.alert('请上传第' + line + '行图文的图片');
                        error = true;
                        return false;
                    }

                    if (v.link == '') {
                        E.alert('请上传第' + line + '行图文的链接');
                        error = true;
                        return false;
                    }

                    if (v.source_title == '') {
                        E.alert('请上传第' + line + '行图文的标题');
                        error = true;
                        return false;
                    }

                    html += '<div><a href="' + v.url_link + '"><img src="' + v.img_link + '" width="100%" /></a></div>'

                });

                if (error) {
                    return false;
                }

                html += '</div>';
                html += '</div>';

                html += '<ul id="position">';
                $.each(template.tempSource, function(k, v) {
                    if (k == 0) {
                        html += '<li class="cur"></li>';
                    } else {
                        html += '<li></li>';
                    }
                });
                html += '</ul>';

            } else if (template.module == 'html') {

                var html = $('#template-description').val();
                if (E.isEmpty(html)) {
                    E.alert('请录入html内容');
                    return false;
                }
                template_source[template.module] = new Array({
                    source_title: 'html内容',
                    description: html
                });

            }else if(template.module == 'nav_wap'){

                template_source[template.module] = template.tempSource;

                if (template.tempSource.length == 0) {
                    E.alert('请录入菜单信息');
                    return false;
                }

                var error = false;

                var html = '';
                $.each(template.tempSource, function(k, v) {

                    var line = k + 1;

                    if (v.source_title == '') {
                        E.alert('请上传第' + line + '行中文标题');
                        error = true;
                        return false;
                    }

                    if (v.link == '') {
                        E.alert('请上传第' + line + '行URL链接');
                        error = true;
                        return false;
                    }

                    if (v.description == '') {
                        E.alert('请上传第' + line + '行法文标题');
                        error = true;
                        return false;
                    }

                });

                if (error) {
                    return false;
                }

            }else if(template.module == 'flash_wap_new_product'){

                template_source[template.module] = template.tempSource;

                if (template.tempSource.length == 0) {
                    E.alert('请录入图片信息');
                    return false;
                }

                var error = false;
                var html = '<div class="swipe" id="mySwipe">';
                html += '<div class="swipe-wrap">';
                $.each(template.tempSource, function(k, v) {

                    var line = k + 1;

                    if (v.img_link == '/quanliadmin/docroot/images/backend/comm_default.jpg' || v.img_link == '') {
                        E.alert('请上传第' + line + '行图文的图片');
                        error = true;
                        return false;
                    }

                    if (v.url_link == '') {
                        E.alert('请上传第' + line + '行图文的链接');
                        error = true;
                        return false;
                    }

                    if (v.source_title == '') {
                        E.alert('请上传第' + line + '行图文的标题');
                        error = true;
                        return false;
                    }

                    if (v.description == '') {
                        E.alert('请上传第' + line + '行商品介绍');
                        error = true;
                        return false;
                    }

                    html += '<div><a href="' + v.url_link + '"><img src="' + v.img_link + '" width="100%" /></a></div>'

                });

                if (error) {
                    return false;
                }

                html += '</div>';
                html += '</div>';

            }

            $('#' + template.boxID).html(html);
            E.popup.close();

        },

        check: function() {

            if (E.isEmpty(template_source)) {
                E.alert('请完善页面内容');
            } else {
                E.confirm('您确认要编辑前端模板吗？', 'template.edit.execute');
            }

        },

        execute: function() {

            E.loadding.open('正在保存，请稍候...');

            E.ajax_post({
                url: '/invokingAjax-backend/content/source.ajax?operFlg=1',
                data: {
                    page_type: tempate_page_type,
                    cityID: tempate_city_id,
                    source_data: template_source
                },
                call: 'template.edit.result'
            });

        },

        refreshCache: function() {

            E.loadding.open('正在操作，请稍候...');

            E.ajax_post({
                url: '/invokingAjax-backend/content/source.ajax?operFlg=2',
                data: {
                    page_type: tempate_page_type,
                    cityID: tempate_city_id
                },
                call: 'template.edit.result'
            });

        },

        result: function( o ) {

            E.loadding.close();

            if (o.code == 200) {
                E.alert(o.message, 2);
            } else {
                E.alert(o.message);
            }

        }

    }

};


$(function() {

    //鼠标移动到资源位块
    $('.ebsig-edit').mouseenter(function() {

        $('#template_popup').css('top', $(this).offset().top + 'px')
            .css('left', $(this).offset().left + 'px')
            .css('width', ($(this).width() - 5) + 'px')
            .css('height', $(this).height() + 'px').show();

        template.module = $(this).attr('ebsig-module');
        template.boxID = $(this).attr('id');

    });

    //修改资源位
    $('#template_popup').find('.edit').click(function() {

        template.edit.open();

    });

    //根据商品编号查询商品信息
    $(document).on('keypress', '#product_code', function(event) {
        if (event.keyCode == '13') {
            event.preventDefault();
            var product_code = E.trim($(this).val());
            if (product_code == '') {
                E.alert('请输入商品SKU', 1, function() {
                    $('#product_code').focus();
                });
            } else {
                E.loadding.open('正在查询商品，请稍候...');
                E.ajax_get({
                    url: '/invokingAjax-comm/goods/commGoods.ajax?operFlg=1',
                    data: {
                        product_code: product_code,
                        cityID: tempate_city_id
                    },
                    call: function( o ) {
                        E.loadding.close();
                        if (o.code == 200) {

                            var msg = '';
                            $.each(template.tempSource, function(k, v) {
                                if (v.twoPostID == o.data.twoPostID) {
                                    msg = '已有同款商品，不能重复录入';
                                    return false;
                                }
                            });
                            if (msg != '') {
                                E.alert(msg, 1, function() {
                                    $('#product_code').focus();
                                });
                                return false;
                            }

                            var goods_obj = {
                                id: '',
                                source_title: o.data.goodsName,
                                frenchName: o.data.frenchName,
                                spec: o.data.spec,
                                img_link: o.data.img_link,
                                link: o.data.link,
                                postID: o.data.postID,
                                twoPostID: o.data.twoPostID,
                                price: o.data.salePrice,
                                tag_img_link: o.data.tag_img_link
                            };

                            template.tempSource.push( goods_obj );
                            template.updateHtml();
                            $('#product_code').focus();

                        } else {

                            E.alert(o.message);

                        }
                    }
                })
            }
        }
    }).on('focus', '#product_code', function() {
        var product_code = E.trim($(this).val());
        if (product_code == '请输入商品SKU，按回车键查询') {
            $(this).val('').css('color', '#000');
        }
    }).on('blur', '#product_code', function() {
        var product_code = E.trim($(this).val());
        if (product_code == '') {
            $(this).val('请输入商品SKU，按回车键查询').css('color', '#ccc');
        }
    });

    //删除资源位
    $(document).on('click', '#e_popup .source-delete', function() {

        var index = parseInt($(this).attr('index'));
        E.confirm('您确认要删除该项数据吗？', function() {
            template.tempSource.splice(index, 1);
            template.updateHtml();
        });

    });

    $(document).on('click', '#e_popup .up-btn', function() {

        var index = parseInt($(this).attr('index'));
        if (index == 0) {
            return false;
        }

        var up_index = index - 1;
        var up_data = template.tempSource[up_index];

        template.tempSource[up_index] = template.tempSource[index];
        template.tempSource[index] = up_data;
        template.updateHtml();

    });

    $(document).on('click', '#e_popup .down-btn', function() {

        var index = parseInt($(this).attr('index'));
        if (index == parseInt($('tbody.source-box').length) - 1) {
            return false;
        }

        var down_index = index + 1;
        var down_data = template.tempSource[down_index];

        template.tempSource[down_index] = template.tempSource[index];
        template.tempSource[index] = down_data;
        template.updateHtml();

    });

    //添加图片
    $(document).on('click', '#img_add', function() {

        var source_obj = {
            id: '',
            source_title: '',
            img_link: '',
            url_link: '',
            description: '',
            postID: 0
        };
        template.tempSource.push( source_obj );
        template.updateHtml();

    });

    //添加菜单
    $(document).on('click', '#nav_add', function(){
        var source_obj = {
            id: '',
            source_title: '',
            description: '',
            url_link: '',
            postID: 0
        };
        template.tempSource.push( source_obj );
        template.updateHtml();
    });

    //链接输入框失去焦点
    $(document).on('blur', '.ebsig-link', function() {
        var index = parseInt($(this).attr('index'));
        var url_link = $(this).val();
        template.tempSource[index].url_link = url_link;
    });

    //标题输入框失去焦点
    $(document).on('blur', '.ebsig-title', function() {
        var index = parseInt($(this).attr('index'));
        var source_title = $(this).val();
        template.tempSource[index].source_title = source_title;
    });

    //标题输入框失去焦点
    $(document).on('blur', '.ebsig-description', function() {
        var index = parseInt($(this).attr('index'));
        var source_description = $(this).val();
        template.tempSource[index].description = source_description;
    });

});

//绑定上传图片
KindEditor.ready(function(K) {
    var editor = K.editor({
        allowFileManager : true,
        uploadJson: '/framework/kindeditor/php/upload_json.php?act=content'
    });

    $(document).on('click', '#e_popup .upload_img', function() {

        var o = $(this);
        var index = parseInt(o.attr('index'));

        editor.loadPlugin('image', function() {
            editor.plugin.imageDialog({
                showRemote : false,
                clickFn : function(url) {

                    o.prev().attr('src', url);
                    template.tempSource[index].img_link = url;
                    editor.hideDialog();
                }
            });
        });
    });
});